<?php 

class engagement_crt extends CI_Controller {  
       
	    function __construct()
    {
        parent::__construct();
         $this->load->library("pagination");
		$this->load->helper(array('form', 'url'));
		$this->load->library('image_lib');
		$this->load->helper("file");
        $this->load->helper('form');

    }
	  
	 function index()
	{
		 $this->load->library('session');
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
		$this->load->model('engagement_mdl'); // load the file welcome_mdl.php
		$charticle = $this->charticle_mdl->get_all(); // call the method filtera inside the class welcome_mdl
	    $data_charticle = array('data_charticle'=>$charticle); //ajouter la table r1 a data		
	   $this->session->set_userdata($data_charticle); // mettre $data_charticle dans une var globale
	   
	    $config = array();
          $config["base_url"] = base_url( "engagement_crt/index");
		  $config["total_rows"] =$this->engagement_mdl->record_count_all();
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 
        $this->pagination->initialize($config);
		 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
       $data["results"] = $this->engagement_mdl->get_all($config["per_page"], $page);
	   $data["results_recherche"] = $this->engagement_mdl->get_all_recherche();
        $data["links"] = $this->pagination->create_links();
		$data['titre']='قائمة الالتزامات الكلية';
		  $this->load->view('engagement_vew',$data);  // send the result from the model to the view 
		  
	
	}
	
	
    public function find_name()
    {
		$this->load->model('engagement_mdl'); // load the file welcome_mdl.php
		$nom_fournisseur=$this->input->post('nom');
		$data["results"] = $this->engagement_mdl->get_per_fournisseur_recherche($nom_fournisseur);
		 $data["results_recherche"] = $this->engagement_mdl->get_all_recherche();
		 
		  $config = array();
         $config["base_url"] = base_url("engagement_crt/find_name");
		  $config["total_rows"] =$this->engagement_mdl->record_count_fournisseur($nom_fournisseur);
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
         $this->pagination->initialize($config);
	     
		  $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		 
	
          $data["links"] = $this->pagination->create_links();
		  $data['titre']='الاتزامات الخاصة بالمورد <br> '.$nom_fournisseur;
		 $this->load->view('engagement_vew',$data);  // send the result from the model to the view 
		}
		
	public function get_engagement_per_article2($x1)
    {
		$this->load->model('engagement_mdl'); // load the file welcome_mdl.php
	 $this->load->library('session');
	  $tab['val']=$x1;
	   $tab['nbr-enreg']= $this->engagement_mdl->record_count($x1);
	   $tab['premier_appelle']=TRUE;
	  $this->session->set_userdata($tab); // mettre $tab dans une var globale
	  
	  

		
		$this->get_engagement_per_article3();
		
	
	
    }
	
	
	
	public function get_engagement_per_article3()
    {
		
	 $this->load->library('session');
        $x1=$this->session->userdata('val'); //access la var globale val

		$this->load->model('engagement_mdl'); // load the file welcome_mdl.php
		$this->load->model('charticle_mdl'); // load the file welcome_mdl.php
			 /////pagination
		   $config = array();
         $config["base_url"] = base_url("engagement_crt/get_engagement_per_article3");
		  $config["total_rows"] =$this->engagement_mdl->record_count($x1);
		  $config["per_page"] = 20;
          $config["uri_segment"] = 3;
 //
       $this->pagination->initialize($config);
		   $premier=$this->session->userdata('premier_appelle'); //access la var globale val
	 if($premier){
	 	$page=0;
		 $tab['premier_appelle']=FALSE;
		 $this->session->set_userdata($tab);
		}
	else	 $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		 
	
       $data["results"] = $this->engagement_mdl->get_all2($config["per_page"], $page ,$x1);
	   $data["results_recherche"] = $this->engagement_mdl->get_per_art_recherche($x1);
        $data["links"] = $this->pagination->create_links();
		
		$tt=$data["results"];
		if($tt){
			$nom_article=$tt[0]->nom;
			$data['titre']='الالتزامات الخاصة بالمادة '.$nom_article;
		}else{
			$art = $this->charticle_mdl->get_nom_article($x1);
			$data['titre']='لا يوجد التزامات خاصة بالمادة '.$art[0]->nom ;
		}
		 $this->load->view("engagement_vew",$data);	
	
    }
	
	
		public function detaille_engagement($id)
    {
		$this->load->model('engagement_mdl'); // load the file engagement
		$this->load->model('bc_mdl'); // load the file engagement
		$this->load->model('convention_mdl'); // load the file engagement
		
	
	 
	 $les_engagement= $this->engagement_mdl->get_engagement_detaille($id); 
	 $les_bc= $this->engagement_mdl->get_bcs($id);
	 $les_convention= $this->engagement_mdl->get_conventions($id);
	 
	 $montant_bc=0;
	 if($les_bc){
	 	for($i=0;$i<sizeof($les_bc);$i++){
			$id_bc=$les_bc[$i]->id_bc;
			$tmp_bc=$this->bc_mdl->get_detaille($id_bc);
			if(array_key_exists('quantite',$tmp_bc[0])){
	 	 foreach($tmp_bc as $data) { 
           $montant_bc=$montant_bc +($data->quantite * $data->bc_produit_pu);
	        }
		}
	 }	
	 }
	 
	 $montant_convention=0;
	 if($les_convention){
	 	for($i=0;$i<sizeof($les_convention);$i++){
			$id_convention=$les_convention[$i]->id_convetion;
			$tmp_convention=$this->convention_mdl->get_convention_detaille($id_convention);
			if(array_key_exists('quantite',$tmp_convention[0])){
	 	 foreach($tmp_convention as $data) { 
           $montant_convention=$montant_convention +($data->quantite * $data->pu_convention_produit);
	        }
		}
	 }	
	 }
	 
	  
	  
	  if($les_engagement[0]->id_rapport_transfert_fk){
	 	$this->load->model('etat_financier_mdl'); 
	 	$transfert=$this->etat_financier_mdl->get_detaille_transfert($les_engagement[0]->id_rapport_transfert_fk);
		$montant=$transfert[0]->montant;
	 }else $montant=$montant_bc+$montant_convention; 
	 
	
	 $datas['engagement']=$les_engagement;
	  $datas['bcs']=$les_bc;
	  $datas['conventions']=$les_convention;
	  $datas['montant']=$montant;
	  
     $this->load->view('engagement_detaille_vew',$datas); 
    
	
    }

     	public function imprimer($id)
    {
		$this->load->model('engagement_mdl'); // load the file engagement
		$this->load->model('bc_mdl'); // load the file engagement
		$this->load->model('convention_mdl'); // load the file engagement
		
	
	 
	 $les_engagement= $this->engagement_mdl->get_engagement_detaille($id); 
	 $les_bc= $this->engagement_mdl->get_bcs($id);
	 $les_convention= $this->engagement_mdl->get_conventions($id);
	 
	  $t_creation=$les_engagement[0]->temps_creation;
	 $id_art=$les_engagement[0]->id_article_fk;
	 $les_eng_ancien=$this->engagement_mdl->get_montant_ancien($id_art,$t_creation);
	 
	 $montant_bc=0;
	 if($les_bc){
	 	for($i=0;$i<sizeof($les_bc);$i++){
			$id_bc=$les_bc[$i]->id_bc;
			$tmp_bc=$this->bc_mdl->get_detaille($id_bc);
			if(array_key_exists('quantite',$tmp_bc[0])){
	 	 foreach($tmp_bc as $data) { 
           $montant_bc=$montant_bc +($data->quantite * $data->bc_produit_pu);
	        }
		}
	 }	
	 }
	
	 $montant_convention=0;
	 if($les_convention){
	 	for($i=0;$i<sizeof($les_convention);$i++){
			$id_convention=$les_convention[$i]->id_convetion;
			$tmp_convention=$this->convention_mdl->get_convention_detaille($id_convention);
			if(array_key_exists('quantite',$tmp_convention[0])){
	 	 foreach($tmp_convention as $data) { 
           $montant_convention=$montant_convention +($data->quantite * $data->pu_convention_produit);
	        }
		}
	 }	
	 }
	  $montant=$montant_bc+$montant_convention;
	  
	  $les_eng_ancien=$this->engagement_mdl->get_montant_ancien($id_art,$t_creation);
	  $this->engagement_mdl->mise_a_jour_montant($id,$montant);
	 $ancien_montant=0;
	 for($i=0;$i<sizeof($les_eng_ancien);$i++){
	 	if($les_eng_ancien[$i]->nature==1){
			$ancien_montant=$ancien_montant-$les_eng_ancien[$i]->montant_engagement;
		}else{
			$ancien_montant=$ancien_montant+$les_eng_ancien[$i]->montant_engagement;
		}
		
	 }
	 if($les_engagement[0]->nature==1)$new_montant=$ancien_montant-$les_engagement[0]->montant_engagement;
	 else$new_montant=$ancien_montant+$les_engagement[0]->montant_engagement;
	 
	 if($les_engagement[0]->id_rapport_transfert_fk){
	 	$this->load->model('etat_financier_mdl'); 
	 	$transfert=$this->etat_financier_mdl->get_detaille_transfert($les_engagement[0]->id_rapport_transfert_fk);
		 $datas['transfert']=$transfert;
	 }
	
	 $datas['engagement']=$les_engagement;
	  $datas['bcs']=$les_bc;
	  $datas['conventions']=$les_convention;
	  $datas['montant']=$montant;
	  $datas['ancien_montant']=$ancien_montant;
	  $datas['new_montant']=$new_montant;
	  
     $this->load->view('engagement_imprimer_vew',$datas); 
    
	
    }

       function ajout_engagement($erreur)
	{
		 
		$this->load->model('charticle_mdl'); // load 
	    $result['charticles'] = $this->charticle_mdl->get_all();   // get all  for autocomplete
		
	    $result['erreur']=$erreur;
	
		$this->load->view('engagement_ajout_vew',$result); 
	}

     public function valider_ajout_engagement()
    {
		
		$this->load->model('engagement_mdl'); // load 
		$this->load->model('charticle_mdl'); // load 
		$this->load->model('bc_mdl'); // load 
		
		
		
		$nature= $this->input->post('nature');
		$article= $this->input->post('article');
		$numero_engagement=0;
		$article_existe=$this->charticle_mdl->verifie_article($article);
		//$engagement = $this->engagement_mdl->verifier_num_enga($article,$num_engagement);
		
		if(!$article)$erreur['article']=TRUE;
		elseif($article_existe==0)$erreur['article']=TRUE;//nombre de articles = 0 veut dire pas article de ce nom
		
	
		if(empty($erreur)){ // si engagement ne existe pas
		        
				$today = getdate();
				$date=$today['year'].'-'.$today['mon'].'-'.$today['mday'];
				$id_art=$this->charticle_mdl->get_id_article($article);
				$id_article=$id_art[0]->id_article;
					
				    $tmp=$this->engagement_mdl-> get_engagement_numero($id_article);
					if($tmp){
						$numero_engagement=$tmp[0]->numero;
						$numero_engagement=$numero_engagement+1;
					}else{
						$numero_engagement=2;
					}
		
				$engagement = $this->engagement_mdl->verifier_num_enga($article,$numero_engagement);
               if(!$engagement)$this->engagement_mdl->ajout_engagement($id_article,$nature,$date,$numero_engagement);
				$this->index();
		}
		else{ // si  engagement existe
			 
			//echo "<script> var reponse =confirm(\"يوجد التزام بهذا الرقم\")</script>";
			
			$this->ajout_engagement($erreur);
		}
		
    }

    

}
